package date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/6/13
 * Time: 20:19
 */

public class LocalDateTime_Demo {
    public static void main1(String[] args) {
        LocalDateTime time1 = LocalDateTime.now();
        System.out.println(time1);//2025-06-13T22:58:00.587917400
        LocalDateTime time2 = LocalDateTime.of(2025, 6, 13, 22, 57);
        System.out.println(time2);//2025-06-13T22:57
    }
    public static void main2(String[] args) {
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        LocalDateTime dateTime = LocalDateTime.of(date, time);
        System.out.println(dateTime);//2025-06-13T23:01:00.030160400
    }
    public static void main3(String[] args) {
        LocalDateTime parsedDateTime = LocalDateTime.parse("2026-06-13T23:03:00");
        System.out.println(parsedDateTime);//2026-06-13T23:03
    }
    public static void main4(String[] args) {
        String stringDate = "2026-06-13 23:03:10";
        //创建⼀个自定义的日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //1.解析字符串为LocalDateTime
        LocalDateTime dateTime = LocalDateTime.parse(stringDate, formatter);
        System.out.println(dateTime);//2026-06-13T23:03:10
        //2.格式化LocalDateTime为字符串
        String stringTime = dateTime.format(formatter);
        System.out.println(stringTime);//2026-06-13 23:03:10
    }
    public static void main5(String[] args) {
        LocalDateTime time = LocalDateTime.now();
        //获取年
        int year = time.getYear();
        //获取月
        int month1 = time.getMonthValue();
        Month month2 = time.getMonth();
        //获取日
        int day = time.getDayOfMonth();
        //获取时
        int hour = time.getHour();
        //获取分
        int minute = time.getMinute();
        //获取秒
        int second = time.getSecond();
        System.out.println(year);//2025
        System.out.println(month1);//6
        System.out.println(month2);//JUNE
        System.out.println(day);//13
        System.out.println(hour);//23
        System.out.println(minute);//7
        System.out.println(second);//10
    }
    public static void main6(String[] args) {
        LocalDate date = LocalDate.now();
        System.out.println(date);//2025-06-13
        //本周第几天
        int dayOfWeek = date.getDayOfWeek().getValue();
        System.out.println(dayOfWeek);//5,表示星期五
        //本月第几天
        int dayOfMonth = date.getDayOfMonth();
        System.out.println(dayOfMonth);//13
        //今年第几天
        int dayOfYear = date.getDayOfYear();
        System.out.println(dayOfYear);//164
    }
    public static void main7(String[] args) {
        LocalDateTime date = LocalDateTime.now();
        System.out.println("当前日期：" + date);
        LocalDateTime date1 = date.plusDays(1);
        System.out.println("增加一天后的日期：" + date1);
        LocalDateTime date2 = date.minusDays(1);
        System.out.println("减少一天后的日期：" + date2);
    }
    public static void main8(String[] args) {
        LocalDateTime date = LocalDateTime.now();
        System.out.println("当前日期：" + date);
        LocalDateTime date1 = date.plusWeeks(1);
        System.out.println("增加一周后的日期：" + date1);
        LocalDateTime date2 = date.minusWeeks(1);
        System.out.println("减少一周后的日期：" + date2);
    }
    public static void main9(String[] args) {
        LocalDateTime date = LocalDateTime.now();
        System.out.println("当前日期：" + date);
        LocalDateTime date1 = date.plusMonths(1);
        System.out.println("增加一月后的日期：" + date1);
        LocalDateTime date2 = date.minusMonths(1);
        System.out.println("减少一月后的日期：" + date2);
    }
    public static void main10(String[] args) {
        LocalDateTime date = LocalDateTime.now();
        System.out.println("当前日期：" + date);
        LocalDateTime date1 = date.plusYears(1);
        System.out.println("增加一年后的日期：" + date1);
        LocalDateTime date2 = date.minusYears(1);
        System.out.println("减少一年后的日期：" + date2);
    }
    public static void main11(String[] args) {
        LocalDateTime curDate = LocalDateTime.now();
        System.out.println("当前日期：" + curDate);
        LocalDateTime firstDayOfWeek = curDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        System.out.println("这周的周一：" + firstDayOfWeek);
        LocalDateTime lastDayOfWeek = curDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        System.out.println("这周的周天：" + lastDayOfWeek);
    }
    public static void main(String[] args) {
        LocalDateTime curDate = LocalDateTime.now();
        System.out.println("当前日期：" + curDate);
        LocalDateTime firstDayOfMonth = curDate.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println("本月的第一天：" + firstDayOfMonth);
        LocalDateTime lastDayOfMonth = curDate.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("本月的最后一天：" + lastDayOfMonth);
    }
}
